<template>
  <div>
    <el-form class="input-form"
             label-position="right"
             label-width="120px"
             ref="editForm"
             :rules="propRules"
             :disabled="propReadonly"
             :model="form"
    >
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="品名" prop="matrName">
            <!--<el-input v-model="form.matrName"/>-->
            <matr-select
              v-model="form.matrId"
              :propRows="propDictionaries.matrId"
              :propDisabled="propReadonly"
              @select="handleMatrSelect"
              ref="matrExtSelect"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="品规" prop="matrSpec">
            <el-input v-model="form.matrSpec" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="单品单位" prop="matrUnit">
            <el-input v-model="form.skuUnit" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单品数量" prop="skuQty">
            <el-input v-model.number="form.skuQty" ref="qty" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单品单价" prop="skuPrice">
            <el-input v-model.number="form.skuPrice" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="包装单位" prop="packageUnit">
            <!--<el-input v-model="form.packageUnit" :disabled="strings.isBlank(form.matrId)"/>-->
            <!--<el-select v-model="form.packageUnit" @change="handlePackageUnitChange" :disabled="strings.isBlank(form.matrId)">
              <el-option v-for="pu in packageUnits" :key="pu.unit" :value="pu.unit" :label="pu.unit" ></el-option>
            </el-select>-->
            <matr-unit-select
              ref="matrUnitGrid"
              v-model="form.packageUnit"
              propUrl="/base/matrUnit/list"
              @select="handlePackageUnitChange"
              :propMatrId="form.matrId"
              propValueField="unit"
              :propDisabled="strings.isBlank(form.matrId)"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="包装数量" prop="packageQty">
            <el-input v-model.number="form.packageQty" :disabled="strings.isBlank(form.packageUnit)"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="包装比率" prop="packageRate">
            <el-input v-model.number="form.packageRate" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="小计(元)" prop="amount">
            <el-input v-model="form.amount" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="3"/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
  </div>
</template>

<script>

import MatrSelect from '@/components/base/MatrSelect'
import MatrUnitSelect from '@/components/base/MatrUnitSelect'

export default {
  data() {
    return {
      form: {},
      viewData: {},
      packageUnits: [],
      selectMatrColumns: [
        { name: 'name', title: '品名', span: 6, search: true  },
        { name: 'spec', title: '品规', span: 6, search: true  },
        { name: 'mfgName', title: '厂商名称', span: 6, search: true  },
      ],
    }
  },
  methods: {
    handleMatrSelect(item) {
      let { name, spec, model, skuUnit, skuPrice, stockCount} = item

      this.$set(this.form, 'matrName', name)
      this.$set(this.form, 'matrSpec', spec)
      this.$set(this.form, 'matrModel', model)
      this.$set(this.form, 'skuUnit', skuUnit)
      this.$set(this.form, 'skuPrice', skuPrice)
      this.$set(this.form, 'stockCount', skuPrice)

      // this.$set(this.form, 'packageUnit', null)
      this.$refs.matrUnitGrid.handleFocus()
    },
    handlePackageUnitChange(item) {
      // let item = this.packageUnits.filter(pu => pu.unit == unit)[0]
      if (!item) {
        this.$set(this.form, 'packageUnit', null)
        this.$set(this.form, 'packageRate', null)
        return
      }
      this.$set(this.form, 'packageRate', item.rate)
      this.$set(this.form, 'packageQty', 1)
      this.$refs.qty.$el.querySelector('input').focus()
    }
  },
  computed: {
    cpPackageQty() {
      return this.form.packageQty
    }

  },
  watch: {
    cpPackageQty() {
      let { packageRate, packageQty, skuPrice} = this.form
      this.$set(this.form, 'skuQty', (packageRate && packageQty) ? packageRate * packageQty : 1)
      this.$set(this.form, 'amount', (packageRate && packageQty && skuPrice) ? packageRate * packageQty * skuPrice : 0)
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        this.$set(this.form, 'demandId', initData.id)
        if (!this.form.id) { //新增时直接弹出耗材选择框
          this.$refs.matrExtSelect.handleFocus()
        }
      },
      afterSave: () => {
        // 用作返回自动主键
        this.$set(this.form, this.propPrimaryKey, this.propViewFormData[this.propPrimaryKey])
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  components: {
    MatrSelect,
    MatrUnitSelect
  },
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100%;
  }
</style>
